import router from "@/router/index";
import store from "@/store/index";

export default {
  install(Vue) {
    Vue.prototype.$tabbarClose = function (path = router.currentRoute.fullPath, params) {
      const activedTabPath = router.currentRoute.fullPath;
      // 如果关闭的标签正好是当前路由，并且标签栏数目大于 1
      if (path == activedTabPath && store.state.tabbar.list.length > 1) {
        const index = ~~Object.keys(store.state.tabbar.list).find((i) => {
          return store.state.tabbar.list[i].path == path;
        });
        if (index < store.state.tabbar.list.length - 1) {
          this.$router.push(store.state.tabbar.list[index + 1].path);
        } else {
          this.$router.push(store.state.tabbar.list[index - 1].path);
        }
      }
      store.dispatch("tabbar/remove", path);
    };

    Vue.prototype.$tabbarCloseTo = function (params) {
      const { targetPath, currentPath = router.currentRoute.fullPath } = params || {};
      // 如果关闭的标签正好是当前路由，并且标签栏数目大于 1
      if (targetPath) {
        this.$router.push(targetPath);
      } else {
        const activedTabPath = router.currentRoute.fullPath;
        if (currentPath == activedTabPath && store.state.tabbar.list.length > 1) {
          const index = ~~Object.keys(store.state.tabbar.list).find((i) => {
            return store.state.tabbar.list[i].path == currentPath;
          });
          if (index < store.state.tabbar.list.length - 1) {
            this.$router.push(store.state.tabbar.list[index + 1].path);
          } else {
            this.$router.push(store.state.tabbar.list[index - 1].path);
          }
        }
      }
      store.dispatch("tabbar/remove", currentPath);
    };
  },
};
